package portal.controller;

import beans.LogEnum;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import portal.beans.Gpu;
import portal.dao.ScheduleGpuDetailDao;
import utils.UserLogUtil;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;

/**
 * Created with IDEA
 * author:HMY
 * Date:2018/11/2
 * Time:19:28
 * //TODO
 */
@Component
public class GpuServlet extends BaseServlet {

    private static final long serialVersionUID = -3415439945764547472L;
    @Autowired
    public ScheduleGpuDetailDao scheduleGpuDetailDao;
    
    public void init(ServletConfig config) throws ServletException  
    {  
        SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext());  
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String methodName = request.getParameter("method");
        Method method = null;
        try {
            method = getClass().getDeclaredMethod(methodName,HttpServletRequest.class,HttpServletResponse.class);
            method.invoke(this,request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    /**
     * By HMY
     * //TODO 查询所有的Gpu
     */
    protected void getAllGpu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //ScheduleGpuDetailDao scheduleGpuDetailDao = new ScheduleGpuDetailDao();
        List<Gpu> gpuList = scheduleGpuDetailDao.getAllGpu();
        JSONObject httpResult = new JSONObject();
        if(gpuList!=null && gpuList.size()!=0){
            httpResult.put("result","success");
            httpResult.put("message", JSONArray.fromObject(gpuList));
        }else{
            httpResult.put("result","false");
            httpResult.put("message","查询失败，请联系相关管理员");
        }
        response.getWriter().print(httpResult.toString());
    }

    /**
     * By HMY
     * //TODO 根据gpu型号查询Gpu信息
     */
    protected void getGpuByVersion(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String gpuVersion = request.getParameter("gpuVersion");
        
        Gpu gpu = scheduleGpuDetailDao.getGpuByVersion(gpuVersion);
        JSONObject httpResult = new JSONObject();
        if(gpu!=null){
            httpResult.put("result","success");
            httpResult.put("message",JSONObject.fromObject(gpu));
        }else{
            httpResult.put("result","false");
            httpResult.put("message","查询失败，请联系相关管理员");
        }
        response.getWriter().print(httpResult.toString());
    }

    /**
     * By HMY
     * //TODO 根据gpuID查询Gpu信息
     */
    protected void getGpuById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int gpuId = Integer.parseInt(request.getParameter("gpuId")==null?"0":request.getParameter("gpuId"));
        //ScheduleGpuDetailDao scheduleGpuDetailDao = new ScheduleGpuDetailDao();
        Gpu gpu = scheduleGpuDetailDao.getGpuById(gpuId);
        JSONObject httpResult = new JSONObject();
        System.out.println("get Gpu");
        if(gpu!=null){
            httpResult.put("result","success");
            httpResult.put("message",JSONObject.fromObject(gpu));
        }else{
            httpResult.put("result","false");
            httpResult.put("message","查询失败，请联系相关管理员");
        }
        response.getWriter().print(httpResult.toString());
    }

    /**
     * By HZ
     * //TODO 注册GPU节点信息
     */
    protected void addGpu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String gpuIdentify = request.getParameter("gpuIdentify");
        String gpuVersion = request.getParameter("gpuVersion");
        String gpuDetail = request.getParameter("gpuDetail");
        Gpu gpu = new Gpu();
        gpu.setGpuIdentify(gpuIdentify);
        gpu.setGpuVersion(gpuVersion);
        gpu.setGpuDetail(gpuDetail);
        //ScheduleGpuDetailDao scheduleGpuDetailDao = new ScheduleGpuDetailDao();
        boolean addFlag = scheduleGpuDetailDao.addGpu(gpu);
        JSONObject result = new JSONObject();
        if (addFlag) {
            result.put("result", "success");
            result.put("message", "GPU节点注册成功");
        } else {
            result.put("result", "false");
            result.put("message", "GPU节点注册失败，请联系相关人员处理");
        }
        response.getWriter().write(result.toString());
    }

    /**
     * By HZ
     * //TODO 修改指定GPU节点信息
     */
    protected void modifyGpu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String gpuIdStr = request.getParameter("gpuId");
        int gpuId = Integer.parseInt(gpuIdStr == null ? "" : gpuIdStr);
        String gpuIdentify = request.getParameter("gpuIdentify");
        String gpuVersion = request.getParameter("gpuVersion");
        String gpuDetail = request.getParameter("gpuDetail");
        Gpu gpu = new Gpu();
        gpu.setGpuId(gpuId);
        gpu.setGpuIdentify(gpuIdentify);
        gpu.setGpuVersion(gpuVersion);
        gpu.setGpuDetail(gpuDetail);
        //ScheduleGpuDetailDao scheduleGpuDetailDao = new ScheduleGpuDetailDao();
        boolean addFlag = scheduleGpuDetailDao.updateGpu(gpu);
        JSONObject result = new JSONObject();
        if (addFlag) {
            result.put("result", "success");
            result.put("message", "修改GPU节点成功");
        } else {
            result.put("result", "false");
            result.put("message", "修改GPU节点信息失败");
        }
        response.getWriter().write(result.toString());
    }
    /**
     * By HZ
     * //TODO 删除指定GPU节点信息
     */
    protected void deleteGpu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String gpuIdStr = request.getParameter("gpuId");
        int gpuId = Integer.parseInt(gpuIdStr == null ? "" : gpuIdStr);
        //ScheduleGpuDetailDao scheduleGpuDetailDao = new ScheduleGpuDetailDao();
        boolean addFlag = scheduleGpuDetailDao.deleteGpu(gpuId);
        JSONObject result = new JSONObject();
        if (addFlag) {
            result.put("result", "success");
            result.put("message", "删除GPU节点成功");
        } else {
            result.put("result", "false");
            result.put("message", "删除GPU节点信息失败");
        }
        response.getWriter().write(result.toString());
    }

    /**
     * By HZ //TODO 分页查询GPU节点
     *
     * @return void
     * @Param [request, response]
     **/
    protected void getPagedGpu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //ScheduleGpuDetailDao scheduleGpuDetailDao = new ScheduleGpuDetailDao();
        Integer onePageCount = Integer.parseInt(request.getParameter("onePageCount"));
        Integer currentPage = Integer.parseInt(request.getParameter("currentPage"));
        List<Object> pagedGpuList = scheduleGpuDetailDao.getPagedGpuList(onePageCount, currentPage);
        int pageNum = scheduleGpuDetailDao.getGpuPageNum(onePageCount);
        JSONObject result = new JSONObject();
        if (pagedGpuList != null && pagedGpuList.size() > 0) {
            result.put("result", "success");
            JSONObject message = new JSONObject();
            message.put("pageNum", pageNum);
            message.put("pagedGpuList", JSONArray.fromObject(pagedGpuList));
            result.put("message", message);
        } else {
            result.put("result", "false");
            result.put("message", "查询不到信息");
        }
        response.getWriter().write(result.toString());
    }

}
